package com.suredy.app.porject.ctrl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suredy.app.porject.constants.FlowStatus;
import com.suredy.app.porject.model.Bid;
import com.suredy.app.porject.model.BidAgency;
import com.suredy.app.porject.model.Biddinger;
import com.suredy.app.porject.service.BidAgencySrv;
import com.suredy.app.porject.service.BidSrv;
import com.suredy.app.porject.service.BiddingerSrv;
import com.suredy.app.porject.service.FlowOfBid2UserSrv;
import com.suredy.app.porject.service.ProjectCompany;
import com.suredy.app.porject.service.ProjectConstants;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.security.entity.OrgEntity;

/**
 * 投标列表
 * 
 * @author VIVID.G
 * @since 2016-1-20
 * @version v0.1
 */
@Controller
@RequestMapping("/bid-list")
public class BidListCtrl extends BaseCtrl {

	@Autowired
	private BiddingerSrv biddingerSrv;

	@Autowired
	private BidAgencySrv bidAgencySrv;

	@Autowired
	private BidSrv bidSrv;

	@Autowired
	private ProjectConstants constants;

	@Autowired
	private FlowOfBid2UserSrv bid2UserSrv;

	@Autowired
	private ProjectCompany company;

	@RequestMapping(value = "/{company}")
	public String list(Bid search, @PathVariable int company, Integer page, Integer pageSize, Model model) {
		String view = "app/bid/bid-list";

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		// agency.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBidAgencyInfo() != null && search.getBidAgencyInfo().getAgency() != null) {
			if ("null".equalsIgnoreCase(search.getBidAgencyInfo().getAgency().getId()))
				search.getBidAgencyInfo().setAgency(null);
		}

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		DetachedCriteria dc = this.bidSrv.getDc(search);
		dc.addOrder(Order.asc("openingTime")); // 开标时间正序

		String c = this.company.getCompanyBy(company - 1);
		if (!StringUtils.isBlank(c))
			dc.add(Restrictions.eq("ownCompany", c));

		List<Bid> data = this.bid2UserSrv.getBidOf(dc, null, null, page, pageSize);

		model.addAttribute("biddingers", this.getBiddingers());
		model.addAttribute("agency", this.getAgency());
		model.addAttribute("bidTypes", this.constants.getBidTypes(null));
		model.addAttribute("bidWays", this.constants.getBidWays(null));
		model.addAttribute("search", search);
		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.bid2UserSrv.getBidCountOf(dc, null, null));
		model.addAttribute("company", company);

		return view;
	}

	@RequestMapping(value = "/all")
	public String list(Bid search, Integer page, Integer pageSize, Model model) {
		String view = "app/bid/bid-all-list";

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		DetachedCriteria dc = this.bidSrv.getDc(search);
		dc.addOrder(Order.asc("openingTime")); // 开标时间正序

		List<Bid> data = this.bid2UserSrv.getBidOf(dc, null, null, page, pageSize);

		model.addAttribute("biddingers", this.getBiddingers());
		model.addAttribute("bidTypes", this.constants.getBidTypes(null));
		model.addAttribute("bidWays", this.constants.getBidWays(null));
		model.addAttribute("search", search);
		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.bid2UserSrv.getBidCountOf(dc, null, null));

		return view;
	}

	@RequestMapping(value = "/todo")
	public String bidTodo(Bid search, Integer page, Integer pageSize, Model model) {
		String view = "app/bid/bid-todo-list";

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		// agency.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBidAgencyInfo() != null && search.getBidAgencyInfo().getAgency() != null) {
			if ("null".equalsIgnoreCase(search.getBidAgencyInfo().getAgency().getId()))
				search.getBidAgencyInfo().setAgency(null);
		}

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		DetachedCriteria dc = this.bidSrv.getDc(search);

		String[] userIds = new String[] {this.getUser().getId()};

		List<Bid> data = this.bid2UserSrv.getBidOf(dc, userIds, true, page, pageSize, FlowStatus.FS1000_BID_CREATED);

		model.addAttribute("biddingers", this.getBiddingers());
		model.addAttribute("agency", this.getAgency());
		model.addAttribute("bidTypes", this.constants.getBidTypes(null));
		model.addAttribute("bidWays", this.constants.getBidWays(null));
		model.addAttribute("search", search);
		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.bid2UserSrv.getBidCountOf(dc, userIds, true));

		return view;
	}

	@RequestMapping(value = "/done")
	public String bidDone(Bid search, Integer page, Integer pageSize, Model model) {
		String view = "app/bid/bid-done-list";

		FlowStatus[] fs = new FlowStatus[] {FlowStatus.FS1001_BID_CONTRACT_CREATED, FlowStatus.FS1002_BID_COMPLETED};

		// biddinger.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBiddingerInfo() != null && search.getBiddingerInfo().getBiddinger() != null) {
			if ("null".equalsIgnoreCase(search.getBiddingerInfo().getBiddinger().getId()))
				search.getBiddingerInfo().setBiddinger(null);
		}

		// agency.id不能传空值，所以前端传来一个字符串null，这里需要去除
		if (search.getBidAgencyInfo() != null && search.getBidAgencyInfo().getAgency() != null) {
			if ("null".equalsIgnoreCase(search.getBidAgencyInfo().getAgency().getId()))
				search.getBidAgencyInfo().setAgency(null);
		}

		if (page == null || page <= 0)
			page = 1;

		if (pageSize == null || pageSize <= 0)
			pageSize = 30;

		DetachedCriteria dc = this.bidSrv.getDc(search);

		// 送交合同专责和完成合同签订都属于已办完
		dc.add(Restrictions.in("flowStatus", fs));

		String[] userIds = new String[] {this.getUser().getId()};

		List<Bid> data = this.bid2UserSrv.getAllBidOf(dc, userIds, null);

		model.addAttribute("biddingers", this.getBiddingers());
		model.addAttribute("agency", this.getAgency());
		model.addAttribute("bidTypes", this.constants.getBidTypes(null));
		model.addAttribute("bidWays", this.constants.getBidWays(null));
		model.addAttribute("search", search);
		model.addAttribute("data", data);
		model.addAttribute("page", page);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("count", this.bid2UserSrv.getBidCountOf(dc, userIds, null));

		return view;
	}

	private List<Biddinger> getBiddingers() {
		Biddinger search = new Biddinger();
		search.setOwner(new OrgEntity());
		search.getOwner().setId(this.getUser().getOrgId());

		List<Biddinger> data = this.biddingerSrv.readByEntity(search);

		return data == null || data.isEmpty() ? null : data;
	}

	private List<BidAgency> getAgency() {
		BidAgency search = new BidAgency();
		search.setOwner(new OrgEntity());
		search.getOwner().setId(this.getUser().getOrgId());

		List<BidAgency> data = this.bidAgencySrv.readByEntity(search);

		return data == null || data.isEmpty() ? null : data;
	}

}
